package com.bub.pay.core.manager.system.impl;

import com.bub.pay.core.base.BaseManagerAbstract;
import com.bub.pay.core.dao.SysLogDao;
import com.bub.pay.core.dao.SysRoleDao;
import com.bub.pay.core.entity.bo.system.SysLogAsyncRecordBO;
import com.bub.pay.core.entity.domain.SysLog;
import com.bub.pay.core.entity.domain.SysRole;
import com.bub.pay.core.entity.domain.SysUser;
import com.bub.pay.core.entity.vo.system.SysLogQueryVo;
import com.bub.pay.core.manager.system.CurrentSystemManager;
import com.bub.pay.core.manager.system.SysLogManager;
import com.bub.pay.core.manager.system.async.SysLogAsyncManager;
import com.bub.pay.framework.common.util.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 操作日志
 */
@Service
public class SysLogManagerImpl extends BaseManagerAbstract<SysLog, SysLogDao> implements SysLogManager {
    @Resource
    private CurrentSystemManager currentSystemManager;
    @Resource
    private SysLogDao sysLogDao;
    @Resource
    private SysRoleDao sysRoleDao;
    @Resource
    private SysLogAsyncManager sysLogAsyncManager;
    @Override
    public List<SysLog> querySysLogList(SysLogQueryVo sysLogQueryBO) {
        Example example = new Example(SysLog.class);
        Example.Criteria criteria = example.createCriteria();
        Long createId = sysLogQueryBO.getCreateId();
        String createName = sysLogQueryBO.getCreateName();
        if (createId != null) {
            criteria.andEqualTo("createId", createId);
        }
        String ip = sysLogQueryBO.getIp();
        if (StringUtils.isNotBlank(ip)) {
            criteria.andEqualTo("ip", ip);
        }
        if (StringUtils.isNotBlank(createName)) {
            criteria.andEqualTo("createName", createName);
        }
        Integer systemId = sysLogQueryBO.getSystemId();
        if (systemId != null) {
            criteria.andEqualTo("systemId", systemId);
        }
        example.setOrderByClause(sysLogQueryBO.getOrderByClause());
        return sysLogDao.selectByExample(example);
    }

    @Override
    public void add(SysUser user, String ip,String typeName, String remarks) {
        List<SysRole> listRoleEntity = sysRoleDao.queryRoleByUserId(user.getId(), user.getUserType());
        List<String> roleList = new ArrayList<>();
        StringBuilder sbRole = new StringBuilder();
        if (!CollectionUtil.isNullOrEmpty(listRoleEntity)) {
            listRoleEntity.forEach(e -> {
                roleList.add(e.getCode());
                sbRole.append(e.getName()+",");
            });
        }

        SysLogAsyncRecordBO sysLog = new SysLogAsyncRecordBO();
        sysLog.setTypeName(typeName);
        sysLog.setInfo(remarks);
        sysLog.setCreateId(user.getId());
        sysLog.setCreateName(user.getUserName());
        sysLog.setCreateDate(new Date());
        sysLog.setUserType(user.getUserType());
        sysLog.setSystemId(currentSystemManager.getModule().getSn());
        sysLog.setSystemName(currentSystemManager.getModule().getDesc());
        sysLog.setCreateRole(sbRole.substring(0,sbRole.length()-1));
        sysLog.setIp(ip);
        /*sysLog.setRequestUrl(requestURL);
        sysLog.setRequestMethod(requestMethod);*/
        /*sysLog.setExecMethodClassName(methodClassName);
        sysLog.setExecMethodParams(methodParamsJSON);*/
        // 异步添加数据
        sysLogAsyncManager.addSysLog(sysLog);

    }
}
